package edu.thu.studio.ormdesigner.editor.figure;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.swt.graphics.Color;

import edu.thu.studio.ormdesigner.editor.figure.border.LineWidthBorder;

public abstract class NodeFigure extends RectangleFigure {
    
    private Figure columns;
    
    private Label nameLabel;
    
    public NodeFigure() {
        this.setLayoutManager(new BorderLayout());
        this.setTableStyle();
        this.createHeader();
        this.createColumn();
    }
    
    public void setName(String name) {
        this.nameLabel.setText(name);
    }
    
    public Figure getColumns() {
        return columns;
    }
    
    public void clearColumns() {
        this.columns.removeAll();
    }
    
    protected void setTableStyle() {
        this.setBackgroundColor(getBackgroundColor());
        this.setBorder(new LineBorder(getBorderColor()));
    }
    
    protected void createHeader() {
        Figure top = new Figure();
        this.add(top, BorderLayout.TOP);
        
        ToolbarLayout topLayout = new ToolbarLayout();
        topLayout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
        topLayout.setStretchMinorAxis(true);
        
        top.setLayoutManager(topLayout);
        
        this.nameLabel = new Label();
        this.nameLabel.setBorder(new MarginBorder(new Insets(5, 20, 5, 20)));
        
        top.add(nameLabel);
        
        Figure separater = new Figure();
        separater.setSize(-1, 1);
        separater.setOpaque(true);
        
        top.add(separater);
    }
    
    protected void createColumn() {
        this.columns = new Figure();
        
        ToolbarLayout layout = new ToolbarLayout();
        layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
        layout.setStretchMinorAxis(true);
        layout.setSpacing(0);
        
        columns.setBorder(new LineWidthBorder(getBorderColor(), 2, 1, 0, 0, 0));
        columns.setLayoutManager(layout);
        
        columns.setBackgroundColor(null);
        columns.setOpaque(false);
        this.add(columns, BorderLayout.CENTER);
    }
    
    @Override
    protected void fillShape(Graphics graphics) {
        graphics.setAlpha(200);
        super.fillShape(graphics);
    }
    
    public abstract String getImageKey();
    
    public abstract Color getBackgroundColor();
    
    public abstract Color getBorderColor();
}
